package clientGUI.userPanels;

import javax.swing.JPanel;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JTextArea;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;

import clientGUI.CollegeClientGUI;
import clientGUI.userPanels.Controllers.RequestController;

import clientserver.Client;

import javax.swing.JButton;
import  college.*;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextPane;


public class ChangeRequestPanel extends UPanel {

	
	private CollegeClientGUI    myframe;  //father JFrame
	private RequestController controller; 
	private JLabel reqIni;
	private JLabel initiator;
	private JComboBox systemCB;
	private JTextArea Cdescription_inp;
	private JTextArea CurrSituation_inp;
	private JTextArea Explan_inp;
	private JLabel Jrid;
	private int rid;
	private JButton btnMainMenu;
	private JButton btnSubButton;

	private JTextPane filesPane;
	private JButton btnSubmit;
	private JLabel ridlbl;
	
	public  void SetFrame( CollegeClientGUI fr)
	{
		myframe=fr;
		
		 // Load Request initiator

	
	}
	
	public void setInitiator( String str)
	{
	
		initiator.setText("Request Initiator: "+str);
	controller.setRequestinitiator(str);
	}
	
	

	
	
	
	public ChangeRequestPanel(Client c) {
	super();
	controller = new RequestController(c);
	controller.setRid(rid);
	
	int id=controller.lastrequestid();
	id++;
	rid=id;
	
	
		
		setLayout(null);
		reqIni = new JLabel("");
		reqIni.setBounds(0, 0, 0, 0);
		add(reqIni);
		
		
		  
		
		systemCB = new JComboBox();
		systemCB.setBounds(330, 29, 120, 22);
		add(systemCB);
		
		
		for(String name:general.is_type){
			systemCB.addItem(name);
		}
		
		initiator = new JLabel("Initiator:");
		initiator.setBounds(0, 0, 0, 0);
		add(initiator);
		
	
		
		JLabel lblInformationSystem = new JLabel("Information System:");
		lblInformationSystem.setBounds(174, 33, 136, 14);
		add(lblInformationSystem);
		
		
		
		filesPane = new JTextPane();
		filesPane.setBounds(0, 78, 108, 136);
		filesPane.setText(controller.getAttachedFiles());
		add(filesPane);
		
		ridlbl = new JLabel("Request ID: N"+rid);
		ridlbl.setBounds(134, 0, 141, 14);
		add(ridlbl);
		
		JLabel lblChangeDescription = new JLabel("Change Description:");
		lblChangeDescription.setBounds(196, 78, 114, 14);
		add(lblChangeDescription);
		
		Cdescription_inp = new JTextArea();
		Cdescription_inp.setBounds(361, 75, 79, 29);
		add(Cdescription_inp);
		
		JLabel lblCurrentSituation = new JLabel("Current Situation:");
		lblCurrentSituation.setBounds(195, 114, 98, 14);
		add(lblCurrentSituation);
		
		CurrSituation_inp = new JTextArea();
		CurrSituation_inp.setBounds(361, 115, 76, 23);
		add(CurrSituation_inp);
		
		JLabel lblExplanation = new JLabel("Explanation:");
		lblExplanation.setBounds(195, 148, 98, 14);
		add(lblExplanation);
		
		Explan_inp = new JTextArea();
		Explan_inp.setBounds(364, 149, 76, 22);
		add(Explan_inp);
		
		
		
		btnMainMenu = new JButton("Main Menu");
		btnMainMenu.setBounds(332, 277, 108, 23);
		btnMainMenu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				myframe.SetMainUserPanel();
			}
		});
		
		JButton btnNewButton_1 = new JButton("Add file to Request");
		btnNewButton_1.setBounds(-5, 225, 136, 23);
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
		
				
				JFileChooser c = new JFileChooser();
			      // Demonstrate "Open" dialog for choose file :
			      int rVal = c.showOpenDialog(myframe);
			      if (rVal == JFileChooser.APPROVE_OPTION) 
			      {
			       
			       int len=(int) c.getSelectedFile().length();
			       if(len>Integer.MAX_VALUE) myframe.alert("File too big to attach!\n Max file size is:"+Integer.MAX_VALUE);
			       else
			       {
			    	   String dir=c.getCurrentDirectory().toString();
			    	   String fn=c.getSelectedFile().getName();
			    	   controller.setRid(rid);
			    	   controller.addfile(dir,fn);
			    	   filesPane.setText(controller.getAttachedFiles());
			    	 
			       }
			       
			       
			       }   //APPROVE
			     
				
				
			}
		});
		add(btnNewButton_1);
		add(btnMainMenu);
	
		
		btnSubmit = new JButton("Submit");
		btnSubmit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			
			
				String cdescription=Cdescription_inp.getText();
				String  currsituation=CurrSituation_inp.getText();
		String explanation= Explan_inp.getText();
		String isystem=(String)systemCB.getSelectedItem();
		Request r= new Request(isystem,cdescription,currsituation,explanation);
		//controller init:
		controller.setRid(rid);
		controller.setRequest(r);
		//controller run:
		controller.SaveRequest();
		controller.saveRequestDetails();
	
	
		String reaction="New request added.Your  request id: "+rid+" \nRemember this number for tracking request processing progress";
		myframe.alert(reaction);
			
			}
			
			
			
		});
		btnSubmit.setBounds(318, 205, 91, 23);
		add(btnSubmit);
		
	

	}
}
